$(function(){
    obj.init();
});

var obj = new function () {
    'use strict';
    return {
        dataGrid: null,
        form: null,
        params: {
            userType: null
        },
        dom: {
            save: null,
            edit:null
        },
        init: function () {
            this.params.userType = xhTool.getQuery('iUserType');

            this.dom.save = $('#bt_form_add');
            this.dom.edit = $('#bt_form_edit');
            this.dataGrid = new xhDataGrid($('#list'), 'Apprelease/getListData', {});
            this.dataGrid.parseToolbar().init().activeBtn($('#bt_list_del'), $('#bt_list_power, #bt_list_assist, #bt_list_school'));

            this.form = new xhForm($('#xh_form'));
            this.form.parse().bindRequired(this.dom.save);

            this.listener();
        },
        listener: function () {
            var $this = this;
            xhExtra.clearLoadSuccess(this.dataGrid);
            $this.dataGrid.onDblClickRow = function (rowIndex, rowData) {
                xhExtra.ajaxRequest('Apprelease/getPlanInfo', {iId: rowData.iId}, {}, function (data) {
                    if (data.bInstall == '已执行') {
                        window.top.xhTop.showError('该任务已经在安装，无法修改!');
                    } else {
                        $this.form.reset().fill(data);
                        BindSelect('version',baseUrl+"Apprelease/getVersion?appid="+data.sAppId, data.version);
                        $("#sAppId").attr("disabled",true);//编辑时应用和版本不编辑
                        $("#version").attr("disabled",true);
                        xhExtra.forwardToForm(true);
                    }
                });
            };
            xhExtra.bindListAdd($this.form);
            xhExtra.bindFormReset($this.form);
            xhExtra.bindFormBack();

            //查找
            xhTool.bindDocKeyDown(
                $('#bt_list_search').click(function (){
                    $this.dataGrid.reload({
                        search: {
                            vague: xhExtra.getValue($('#list_vague'))
                        },
                        type: xhExtra.getValue($('#list_search_type')),
                        startDate: xhExtra.getValue($('#list_create_start')),
                        endDate: xhExtra.getValue($('#list_create_end')),
                    })
                })
            );
            //保存
            $this.dom.save.click(function () {
                $this.funObj().submit();
            });
            //修改
            $this.dom.edit.click(function () {
                $("#sAppId").attr("disabled",false);//可传值
                $("#version").attr("disabled",false);
                $this.funObj().submit();
                $("#sAppId").attr("disabled",true);//再设置成禁止编辑，防止第二次双击时能编辑
                $("#version").attr("disabled",true);
            });
            //删除
            $('#bt_list_del').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().deletePlan();
            });
            //关联学校
            $('#bt_list_school').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().contactSchool();
            });
            //改变应用时获取版本
            $("#sAppId").on("change", function (e) {
                var pid = $("#sAppId").val();
                $("#version").val("").trigger('change');
                if(pid!=''){
                    BindSelect("version", baseUrl+"Apprelease/getVersion?appid="+pid, null);
                }else {
                    BindSelect("version", baseUrl+"Apprelease/getVersion?appid=null", null);
                }
            });

            //绑定字典内容到指定的Select控件
            function BindSelect(ctrlName, url, versiondata) {
                var control = $('#' + ctrlName);
                //设置Select2的处理
                control.select2({
                    placeholder:'请选择',
                    templateResult:  function formatRepo(repo){
                        return repo.text
                    },
                    templateSelection: function (repo) {
                        if(repo.id!=""){
                            $('#select2-'+ctrlName+'-container').parent().removeClass('warn-color').addClass('success-color');
                        }
                        return repo.text;
                    },
                    escapeMarkup: function (m) {
                        $('#select2-'+ctrlName+'-container').parent().addClass('warn-color');
                        return m;
                    }
                });

                //绑定Ajax的内容
                $.getJSON(url, function (data) {
                    control.empty();//清空下拉框
                    // control.append("<option value='' style='display: none;' selected>请选择</option>")
                    if(versiondata == null){
                        control.append("<option value='' style='display: none;width: 100%' selected>请选择</option>")
                        $.each(data, function (id,item) {
                            control.append("<option style='width: 100%' value='" + item['id'] + "'>" + item['text'] + "</option>");
                        });
                    }else {
                        $.each(data, function (id,item) {
                            if(versiondata['id']==item['id']){
                                control.append("<option style='width: 100%' value='" + item['id'] + "' selected>" + item['text'] + "</option>");
                            }else {
                                control.append("<option style='width: 100%' value='" + item['id'] + "'>" + item['text'] + "</option>");
                            }

                        });
                    }

                });
            }
        },
        funObj: function(){
            var $this = this;
            return {
                submit: function () {
                    $this.form.submit('Apprelease/saveReleasePlan', {}, function (data) {
                        var iId = data.iId;
                        var appName = data.appName;
                        var date = data.date;
                        xhExtra.backToList($this.dataGrid);
                        window.top.xhTop.showModal({
                            title: '应用:' + appName + '；发布日期：[' + date + ']',
                            content: xhTool.getFrameString('Apprelease/selectSchool', {iId: iId}, 550),
                            size: 'lg',
                            button: true,
                            backClose: false
                        });
                        xhExtra.backToList($this.dataGrid);
                    });
                },
                deletePlan: function () {
                    xhExtra.askChangeStatus('Apprelease/delReleasePlan', {iId: $this.dataGrid.getSelectValues('iId')}, $this.dataGrid, '确认删除？');
                },
                contactSchool: function () {
                    var iId = $this.dataGrid.getSelectValue('iId');
                    var bDownLoad = $this.dataGrid.getSelectValue('bDownLoad');
                    var bInstall = $this.dataGrid.getSelectValue('bInstall');
                    if (!iId) {
                        window.top.xhTop.showError('选中id有误！');
                    }else{
                        if (bInstall == '已执行') {
                            window.top.xhTop.showError('该任务正在安装或安装完成，请勿修改学校!');
                        } else {
                            window.top.xhTop.showModal({
                                title: '选择学校-' + $this.dataGrid.getSelectValue('sAppName'),
                                content: xhTool.getFrameString('Apprelease/selectSchool', {iId: iId}, 550),
                                size: 'lg',
                                button: true,
                                backClose: false
                            });
                        }
                    }
                }
            }
        }
    }
};